//
//  ForgetPswViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/9.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "ForgetPswViewController.h"
#import "BaseTextField.h"
#import "VerifyCodeBtn.h"
#import "CheckInput.h"
#import "CYXBaseRequest.h"

@interface ForgetPswViewController ()
@property (weak, nonatomic) IBOutlet BaseTextField *phoneInput;
@property (weak, nonatomic) IBOutlet BaseTextField *vCodeInput;
@property (weak, nonatomic) IBOutlet BaseTextField *nPswInput;
@property (weak, nonatomic) IBOutlet BaseTextField *ncPswInput;
@property (weak, nonatomic) IBOutlet VerifyCodeBtn *pvcBtn;

@end

@implementation ForgetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)setupUI{
    [super setupUI];
    self.title = @"重置密码";
    
    self.phoneInput.placeholder = LCLocalString(@"plh_qssjh");
    self.phoneInput.placeholderColor = PlhColor;
    self.vCodeInput.placeholder = LCLocalString(@"plh_qsryzm");
    self.vCodeInput.placeholderColor = PlhColor;
    self.nPswInput.placeholder = LCLocalString(@"plh_qsrxmm");
    self.nPswInput.placeholderColor = PlhColor;
    self.ncPswInput.placeholder = LCLocalString(@"cfm_xmm");
    self.ncPswInput.placeholderColor = PlhColor;
    LRWeakSelf(self);
    self.pvcBtn.clickBlock = ^{
        [weakself getPvCode];
        
//        if (weakself.actionBlock) {
//            weakself.actionBlock(2,@{});
//        }
    };
    [self.pvcBtn configVerifyBtn:60];
}

- (BOOL)checkPhone{
    if (!self.phoneInput.text.length) {
        LCInfoHUD(LCLocalString(@"ts_sjhbnwk"), 1);
        return NO;
    }
    if ([CheckInput isPhoneNum:self.phoneInput.text]) {
        LCInfoHUD(LCLocalString(@"ts_sjhyw"), 1)
        return NO;
    }
    return YES;
}


- (void)getPvCode{
    if (![self checkPhone]) return;
    
        NSDictionary *param = @{@"phone":self.phoneInput.text};
    
        [CYXBaseRequest postWithUrl:API_sendResetPasswordPvc param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                [self.pvcBtn beginRun];
                LCInfoHUD(LCLocalString(@"yzmyfs"), 1);
            }
            
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (BOOL)checkInput{
    if (!self.phoneInput.text.length) {
        LCInfoHUD(LCLocalString(@"ts_sjhbnwk"), 1);
        return NO;
    }
    if (self.vCodeInput.text.length != 6) {
        LCInfoHUD(self.vCodeInput.placeholder, 1);
        return NO;
    }
    if (!self.nPswInput.text.length) {
        LCInfoHUD(self.nPswInput.placeholder, 1);
        return NO;
    }
    if (!self.ncPswInput.text.length) {
        LCInfoHUD(self.ncPswInput.placeholder, 1);
        return NO;
    }
    if (self.nPswInput.text.length < 8 || self.nPswInput.text.length > 20) {
        LCInfoHUD(LCLocalString(@"mmcwts"), 1);
        return NO;
    }
    if (![self.nPswInput.text isEqualToString:self.ncPswInput.text]) {
        LCInfoHUD(LCLocalString(@"lcmmbt"), 1);
        return NO;
    }
    return YES;
}
- (IBAction)doneAction:(id)sender {
  
    NSDictionary *param = @{@"phone":self.phoneInput.text,@"password":self.nPswInput.text,@"code":self.vCodeInput.text};

    [CYXBaseRequest postWithUrl:API_resetPasswordCommit param:param resultClass:nil success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
            LCInfoHUD(@"操作成功", 1);
            [self popVCTo:@"AccountSafeMenuController"];
            
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
    
    
}



@end
